﻿using Agile.Infra.Core.Agile.Extensions;

namespace Agile.Service.Admin.WebApi.Controllers;

/// <summary>
/// 系统字典服务接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class SysDictionaryController : AgileControllerBase
{
    /// <summary> 添加字典类型 </summary> 
    [HttpPost]
    public async Task<ResponseResult<bool>> InsertType(InsertSysDictionaryTypeRequest request)
    {
        var model = request.MapTo<SysDictionaryModel>();
        model.ParentDicCode = string.Empty;
        return Result(await this.GetService<ISysDictionaryService>().InsertAsync(model));
    }

    /// <summary> 修改字典类型 </summary> 
    [HttpPost]
    public async Task<ResponseResult<bool>> UpdateType(UpdateSysDictionaryTypeRequest request)
    {
        var model = request.MapTo<SysDictionaryModel>();
        model.ParentDicCode = string.Empty;
        return Result(await this.GetService<ISysDictionaryService>().UpdateAsync(model));
    }

    /// <summary> 删除字典类型 </summary> 
    [HttpGet]
    public async Task<ResponseResult<bool>> RemoveType(string dicCode)
    => Result(await this.GetService<ISysDictionaryService>().RemoveAsync(string.Empty, dicCode));

    /// <summary> 查询字典类型 </summary> 
    [HttpPost]
    public async Task<ResponseResult<List<QuerySysDictionaryResponse>>> QueryType(QuerySysDictionaryTypeRequest request)
    => Result(await this.GetService<ISysDictionaryService>().QueryAsync(string.Empty, request.Enabled, request.KeyWords));


    /// <summary> 添加字典数据 </summary> 
    [HttpPost]
    public async Task<ResponseResult<bool>> InsertData(InsertSysDictionaryDataRequest request)
    => Result(await this.GetService<ISysDictionaryService>().InsertAsync(request.MapTo<SysDictionaryModel>()));

    /// <summary> 修改字典数据 </summary> 
    [HttpPost]
    public async Task<ResponseResult<bool>> UpdateData(UpdateSysDictionaryDataRequest request)
    => Result(await this.GetService<ISysDictionaryService>().UpdateAsync(request.MapTo<SysDictionaryModel>()));

    /// <summary> 删除字典数据 </summary> 
    [HttpGet]
    public async Task<ResponseResult<bool>> RemoveData(string parentDicCode, string dicCode)
    => Result(await this.GetService<ISysDictionaryService>().RemoveAsync(parentDicCode, dicCode));

    /// <summary> 查询字典数据 </summary> 
    [HttpPost]
    public async Task<ResponseResult<List<QuerySysDictionaryResponse>>> QueryData(QuerySysDictionaryDataRequest request)
    => Result(await this.GetService<ISysDictionaryService>().QueryAsync(request.ParentDicCode, request.Enabled, request.KeyWords));

    /// <summary> 查询字典数据 </summary> 
    [HttpGet]
    public async Task<ResponseResult<List<QueryTopSysDictionaryResponse>>> QueryTopData(string parentDicCode)
    => Result(await this.GetService<ISysDictionaryService>().QueryTopAsync(parentDicCode));
}
